package vista.ventanas;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;

import javax.swing.JSplitPane;

import vista.Tema;
import vista.objetos.DiagramaGraficable;
import vista.objetos.GraficableConEtiqueta;
import vista.paneles.hojaDibujo.HojaDeDibujo;
import vista.treeDiagrama.TreeDeDiagrama;

import java.awt.Toolkit;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

@SuppressWarnings("serial")
public class VentanaEditor extends JFrame implements Printable {
	
	private static final String PATH_LOGO = "/vista/imagenes/logo32.png";
	
	private TreeDeDiagrama arbolDeDiagrama;
	private HojaDeDibujo areaDeDibujo;
	private JToolBar toolBar;
	private Tema tema;
	private JScrollPane panelScrool;
	
	public VentanaEditor(DiagramaGraficable diagramaPpal)
	{
		super();
		inicializar();
		agregarComponentes(diagramaPpal);
		this.setVisible(true);
	}
	
	public JToolBar getToolBar(){
		return this.toolBar;
	}
	
	private void agregarComponentes(DiagramaGraficable diagramaPpal) 
	{
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		
		this.setContentPane(contentPane);
						
		agregarPanelSuperior();
		agregarPanelCentral(diagramaPpal);
	}

	private void agregarPanelCentral(DiagramaGraficable diagramaPpal)
	{
		JSplitPane splitPane = new JSplitPane();
		
		splitPane.setDividerLocation(200);
	    splitPane.setDividerSize(10);
	 
		splitPane.setOneTouchExpandable(true);

		arbolDeDiagrama = new TreeDeDiagrama(diagramaPpal, this);	
		splitPane.setLeftComponent(new JScrollPane(arbolDeDiagrama));
				
		areaDeDibujo = new HojaDeDibujo(diagramaPpal);
		
		JPanel panelFondo = new JPanel();
		panelFondo.add(areaDeDibujo.getCanvas());
		panelFondo.setBackground(this.tema.getFondoColor());
		
		panelScrool = new JScrollPane(panelFondo);
		splitPane.setRightComponent(panelScrool);
		
		this.getContentPane().add(splitPane, BorderLayout.CENTER);
	}

	private void agregarPanelSuperior()
	{
		toolBar = new JToolBar();
		this.getContentPane().add(toolBar, BorderLayout.NORTH);
	}

	private void inicializar()
	{
		this.setTitle("Editor Pangea");
//		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.getContentPane().setLayout(new BorderLayout());

		Dimension fullScreen = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(fullScreen);

		this.setIconImage(Toolkit.getDefaultToolkit().getImage(VentanaEditor.class.getResource(PATH_LOGO)));
		
		this.tema = Tema.getInstancia();
	}
	
	public void agregarObjetoGraficableAlArbol(GraficableConEtiqueta graficable) {
		graficable.agregarseAlArbol(arbolDeDiagrama);
	}

	public void agregarDiagrama(DiagramaGraficable diagrama){
		this.arbolDeDiagrama.agregaDiagrama(diagrama, this);
		this.areaDeDibujo.setDiagramaActual(diagrama);
	}
	
	public void cargarDiagrama(DiagramaGraficable diagrama){
		this.areaDeDibujo.setDiagramaActual(diagrama);
		this.arbolDeDiagrama.cambiarDiagramaActual(diagrama);
	}
	
	public HojaDeDibujo getHojaDeDibujo(){
		return this.areaDeDibujo;
	}
	
	public void actualizarVista() {
		this.arbolDeDiagrama.repaint();
		this.getHojaDeDibujo().getCanvas().display();
	}
	
	public void zoomHoja()
	{
		this.areaDeDibujo.aumentarEscalado();
		this.panelScrool.revalidate();
	}

	public void unZoomHoja()
	{
		this.areaDeDibujo.reducirEscalado();
		this.panelScrool.revalidate();
	}
	
	public void rotarHoja()
	{
		this.areaDeDibujo.cambiarSentidoDeHoja();
		this.panelScrool.revalidate();
	}
	
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException 
    {
        if (pageIndex > 0) 
        { /* We have only one page, and 'page' is zero-based */
          return NO_SUCH_PAGE;
        }

        Component componentToBePrinted = this.getHojaDeDibujo().getCanvas();
        double panelWidth = componentToBePrinted.getWidth();
        double panelHeight = componentToBePrinted.getHeight();
        double height = pageFormat.getImageableHeight();
        double width = pageFormat.getImageableWidth();
        double scale1 = height / panelHeight;
        double scale2 = width / panelWidth;
        Graphics2D g2d = (Graphics2D)graphics;
        
        
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        scale1 = (scale2 < scale1) ? scale2: scale1 ;

       	g2d.scale(scale1, scale1);
        componentToBePrinted.printAll(graphics);

        return PAGE_EXISTS;
    }    
}
